package org.emet.service.storage;

import java.util.List;

import org.apache.log4j.Logger;
import org.emet.dao.storage.VenderDao;
import org.emet.entity.storage.Vender;
import org.emet.jee.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.security.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供应商
 * 
 * @author alex
 * 
 */
@Service
@Transactional
@RemotingDestination
public class VenderManager {
	private Logger logger = Logger.getLogger(VenderManager.class);
	@Autowired
	private VenderDao venderDao;

	@Secured("A_VIEW_VENDER")
	@Transactional(readOnly = true)
	public List<Vender> getAll() {
		try {
			return venderDao.getAll();
		} catch (Exception e) {
			final String message = "获取供应商信息时发生错误!";
			logger.error(message, e);
			throw new BusinessException(message);
		}
	}

	@Secured("A_MODIFY_VENDER")
	public void save(Vender vender) {
		try {
			Vender mergedVender = (Vender) venderDao.getSession().merge(vender);
			venderDao.save(mergedVender);
		} catch (Exception e) {
			final String message = "保存供应商信息时发生错误!";
			logger.error(message, e);
			throw new BusinessException(message);
		}
	}

	@Secured("A_MODIFY_VENDER")
	public void deleteVender(long id) {
		// TODO check if referenced by other places
		try {
			venderDao.delete(id);
		} catch (Exception e) {
			final String message = "删除供应商信息时发生错误!";
			logger.error(message, e);
			throw new BusinessException(message);
		}
	}
}
